﻿using System;
using System.Collections;
using System.Collections.Generic;

using System.Text;
using System.IO;

namespace YukiFX5017
{
    public static class DMaps
    {
        public static int DMapsLoaded = 0;
        public static string DMapPath = @"C:\Users\Boost\Dropbox\Rise2Fame\0 -Tools for everyone-\Rise2Fame Client\map\map";
        public static string DMapPath2 = @"C:\Users\Boost\Dropbox\Rise2Fame\0 -Tools for everyone-\Rise2Fame Client\map\map";
        public static Hashtable H_DMaps = new Hashtable();
        public static bool Loaded = false;
        public static void Load()
        {
            DMapPath = @"C:\Users\" + Environment.UserName + @"\Dropbox\Rise2Fame\0 -Tools for everyone-\Rise2Fame Client\map\map";
            DMapPath2 = @"C:\Users\" + Environment.UserName + @"\Dropbox\Rise2Fame\0 -Tools for everyone-\Rise2Fame Client";
            if (Directory.Exists(DMapPath) && File.Exists(DMapPath+@"\GameMap.dat"))
                {
                    int Maps = 0;
                    General.WriteLine("Starting to load DMaps.");
                    try
                    {
                        FileStream FS = new FileStream(DMapPath+@"\GameMap.dat", FileMode.Open);
                        BinaryReader BR = new BinaryReader(FS);
                        uint MapCount = BR.ReadUInt32();
                        for (uint i = 0; i < MapCount; i++)
                        {
                            ushort MapID = (ushort)BR.ReadUInt32();
                            string Path = Encoding.ASCII.GetString(BR.ReadBytes(BR.ReadInt32()));
                            //if (MapsNeeded.Contains((int)MapID))
                            //{
                                DMap D = new DMap(MapID, Path);
                                H_DMaps.Add(MapID, D);
                                Maps += 1;
                            //}
                            BR.ReadInt32();
                        }
                        BR.Close();
                        FS.Close();
                        Loaded = true;
                        DMapsLoaded = Maps;
                        General.WriteLine("Total DMaps: " + DMapsLoaded);
                    }
                    catch (Exception Exc)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        General.WriteLine(Exc.ToString());
                        Console.ForegroundColor = ConsoleColor.Red;
                        General.WriteLine("DMap Load system encountered a problem! DMaps couldn't be loaded.");
                    }
                }
                else
                    General.WriteLine("DMap couldn't be loaded. Insert DMaps into map/map folder!");
            }
    }
    public struct DMapCell
    {
        private Boolean _noAccess;
        public Boolean High;
        public DMapCell(Boolean noAccess)
        {
            _noAccess = noAccess;
            High = false;
        }

        public Boolean NoAccess
        {
            get
            {
                return _noAccess;
            }

            internal set
            {
                _noAccess = value;
            }
        }
    }
    public class DMap
    {
        private Int32 Width;
        private Int32 Height;
        private DMapCell[,] Cells;

        public DMap(ushort MapID, string Path)
        {
            try
            {
                if (File.Exists(DMaps.DMapPath2 + @"\" + Path))
                {
                    FileStream FS = new FileStream(DMaps.DMapPath2 + @"\" + Path, FileMode.Open);
                    BinaryReader BR = new BinaryReader(FS);
                    BR.ReadBytes(268);
                    Width = BR.ReadInt32();
                    Height = BR.ReadInt32();
                    Cells = new DMapCell[Width, Height];

                    byte[] cell_data = BR.ReadBytes(((6 * Width) + 4) * Height);
                    int offset = 0;

                    for (int y = 0; y < Width; y++)
                    {
                        for (int x = 0; x < Height; x++)
                        {
                            Boolean noAccess = BitConverter.ToBoolean(cell_data, offset) != false;

                            if (MapID == 1002)
                            {
                                if (x >= 606 && x <= 641)
                                    if (y >= 674 && y <= 680)
                                        noAccess = false;
                                if (x >= 148 && x <= 194)
                                    if (y >= 541 && y <= 546)
                                        noAccess = false;
                            }
                            Cells[x, y] = new DMapCell(noAccess);
                            if (MapID == 1038)
                            {
                                if (x <= 119)
                                    Cells[x, y].High = true;
                                if (x >= 120 && x <= 216 && y <= 210)
                                    Cells[x, y].High = true;
                            }
                            offset += 6;

                        }
                        offset += 4;
                    }
                    BR.Close();
                    FS.Close();
                }
                else
                {
                    General.WriteLine("Did not load" + Path);
                }
            }
            catch (InvalidOperationException exc)
            {
                General.WriteLine(Convert.ToString(exc));
            }
        }

        public DMapCell GetCell(ushort X, ushort Y)
        {
            return Cells[X, Y];
        }
    }
}